package com.wc.算法提高课.A第一章_动态规划.单调队列优化DP.最大子序和;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/13 16:22
 * @description https://www.acwing.com/problem/content/137/
 */
public class Main {
    /**
     * 思路：将每一个s[i] - 范围内最小的就好了
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 300010;
    static int[] s = new int[N];
    static int[] q = new int[N];
    static int hh = 1, tt = 0;
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        int res = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            s[i] = sc.nextInt();
            res = Math.max(res, s[i]);
            s[i] += s[i - 1];
        }
        q[++tt] = 0;
        // 单调队列当中是有一个0存在的
        for (int i = 1; i <= n; i++) {
            // 多了一个
            if (q[hh] < i - m) hh++;
            res = Math.max(res, s[i] - s[q[hh]]);
            while (hh <= tt && s[i] <= s[q[tt]]) tt--;
            q[++tt] = i;
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
